﻿using Stee.CAP8.MessageBus.Sagas.Core.Sagas;
using System;

namespace Stee.CAP8.MessageBus.Sagas.Core.Processes
{
    public class SagaScheduleInfo
    {

        public SagaScheduleInfo(ISagaData sagaData,
                               Object commandInstance,
                               Type commandType)
        {
            SagaData = sagaData ?? throw new ArgumentNullException(nameof(sagaData));
            CommandInstance = commandInstance ?? throw new ArgumentNullException(nameof(commandInstance));
            CommandType = commandType ?? throw new ArgumentNullException(nameof(commandType));

        }

        public SagaScheduleInfo(ISagaData sagaData,
                                Object commandInstance,
                                Type commandType,
                                TimeSpan retryInterval,
                                int retryCount) : this(retryInterval, retryCount)
        {
            SagaData = sagaData ?? throw new ArgumentNullException(nameof(sagaData));
            CommandInstance = commandInstance ?? throw new ArgumentNullException(nameof(commandInstance));
            CommandType = commandType ?? throw new ArgumentNullException(nameof(commandType));

        }

        public SagaScheduleInfo(TimeSpan retryInterval, int retryCount = 3)
        {
            LastRunTime = DateTime.Now;
            RetryInterval = retryInterval == null ? TimeSpan.FromSeconds(30) : retryInterval;
            RetryCount = retryCount;
        }

        public ISagaData SagaData { get; set; }
        public Object CommandInstance { get; set; }

        public Type CommandType { get; set; }
        public DateTime LastRunTime { get; set; }
        /// <summary>
        /// 默认30秒执行一次
        /// </summary>
        public TimeSpan RetryInterval { get; set; } = TimeSpan.FromSeconds(30);
        /// <summary>
        /// 重试次数
        /// </summary>
        public int RetryCount { get; set; } = 3;

        public bool IsComplete { get; set; }

        public bool IsTimeOut()
        {
            return (LastRunTime + RetryInterval) >= DateTime.Now;
        }




    }
}
